﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalOut
{
    public class Cal应变_Result
    {
        public string id;
        public string batchId;
        public string equipmentId;
        public string projectId;
        public string CalType;
        public double 温度;
        public double[] 正应力;

        public string Get温度Insert(string batchId, string channelNo
            , int sortNo
            , double 温度
            , string Separation)
        {
            温度 = Math.Round(温度, 2);

            温度 = 温度.ToString() == "NaN" ? 0 : 温度;
            return string.Format(@"INSERT INTO output_temperature{3}(
                batchId,sortNo,temperature,channelNo)
                VALUES('{0}',{1},{2},{4})"
                , batchId, sortNo, 温度
                , Separation, channelNo);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="batchId"></param>
        /// <param name="sortNo"></param>
        /// <param name="正应变"></param>
        /// <param name="通道号"></param>
        /// <returns></returns>
        public string Get应变Insert(string batchId, string channelNo
            , int sortNo
            , double 正应变, string Separation)
        {
            正应变 = Math.Round(正应变, 4);
            正应变 = 正应变.ToString() == "NaN" ? 0 : 正应变;
            return string.Format(@"INSERT INTO output_data{3}(
                batchId,sortNo,正应力,channelNo)
                VALUES('{0}',{1},{2},{4})"
                , batchId, sortNo, 正应变, Separation, channelNo);
        }
        public virtual string GetSelect(string batchId)
        {
            string Separation = string.Format("_{0}", batchId.Split('_')[0]);
            return string.Format(@"SELECT
                sortNo
                ,正应力 as 应变
                FROM output_data{1}
                WHERE batchId = '{0}'
                ORDER BY sortNo
                ", batchId, Separation);
        }
        public string Get温度SQL(string batchId)
        {
            string Separation = string.Format("_{0}", batchId.Split('_')[0]);
            return string.Format(@"SELECT sortNo
                , temperature
                FROM output_temperature{1}
                WHERE batchId = '{0}'
                ORDER BY sortNo
                ", batchId, Separation);
        }
        public Cal应变_Result(string id, string batchId, string equipmentId, string projectId, string calType)
        {
            this.id = id;
            this.batchId = batchId;
            this.equipmentId = equipmentId;
            this.projectId = projectId;
            this.CalType = calType;
        }

        public double GetDoubleRound(double number, int site)
        {
            return Math.Round(number, site);
        }
        public double[] GetDoubleRound(double[] number, int site)
        {
            for (int i = 0; i < number.Length; i++)
            {
                number[i] = GetDoubleRound(number[i], site);
            }
            return number;
        }
        public double[,] GetDoubleRound(double[,] number, int site)
        {
            int len = 6;
            for (int Cal = 0; Cal < len; Cal++)
            {
                for (int Row = 0; Row < len; Row++)
                {
                    number[Cal, Row] = GetDoubleRound(number[Cal, Row], site);
                }
            }
            return number;
        }
    }
    public sealed class Cal应力_Result : Cal应变_Result
    {
        public double[] 局部坐标系应力;
        public double[] ONE次变换;
        public double[] TWO次变换;
        public double[] THREE次变换;
        public double[] 大地坐标系应力;
        public double[] li1;
        public double[] pqθ;
        public double[] 主应力123;
        public double[] li2;
        public double[] mi2;
        public double[] ni2;
        public double[] 主应力倾角β;
        public double[] 主应力方位角α;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="batchId"></param>
        /// <param name="sortNo"></param>
        /// <param name="正应力"></param>
        /// <param name="局部坐标系应力"></param>
        /// <param name="ONE次变换"></param>
        /// <param name="TWO变换"></param>
        /// <param name="THREE变换"></param>
        /// <param name="大地坐标系应力"></param>
        /// <param name="li1"></param>
        /// <param name="pqθ"></param>
        /// <param name="主应力123"></param>
        /// <param name="li2"></param>
        /// <param name="mi2"></param>
        /// <param name="ni2"></param>
        /// <param name="主应力倾角β"></param>
        /// <param name="主应力方位角α"></param>
        /// <returns></returns>
        public string Get应力Insert(string batchId
            , int sortNo
            , double 正应力
            , double 局部坐标系应力
            , double ONE次变换
            , double TWO变换
            , double THREE变换
            , double 大地坐标系应力
            , double li1
            , double pqθ
            , double 主应力123
            , double li2
            , double mi2
            , double ni2
            , double 主应力倾角β
            , double 主应力方位角α
            , string Separation
            , string channelNo)
        {
            正应力 = Math.Round(正应力, 4);
            局部坐标系应力 = Math.Round(局部坐标系应力, 4);
            ONE次变换 = Math.Round(ONE次变换, 2);
            TWO变换 = Math.Round(TWO变换, 2);
            THREE变换 = Math.Round(THREE变换, 2);
            大地坐标系应力 = Math.Round(大地坐标系应力, 2);
            li1 = Math.Round(li1, 2);
            pqθ = Math.Round(pqθ, 2);
            主应力123 = Math.Round(主应力123, 2);
            li2 = Math.Round(li2, 4);
            mi2 = Math.Round(mi2, 2);
            ni2 = Math.Round(ni2, 2);
            主应力倾角β = Math.Round(主应力倾角β, 2);
            主应力方位角α = Math.Round(主应力方位角α, 2);

            正应力 = 正应力.ToString() == "NaN" ? 0 : 正应力;
            局部坐标系应力 = 局部坐标系应力.ToString() == "NaN" ? 0 : 局部坐标系应力;
            ONE次变换 = ONE次变换.ToString() == "NaN" ? 0 : ONE次变换;
            TWO变换 = TWO变换.ToString() == "NaN" ? 0 : TWO变换;
            THREE变换 = THREE变换.ToString() == "NaN" ? 0 : THREE变换;
            大地坐标系应力 = 大地坐标系应力.ToString() == "NaN" ? 0 : 大地坐标系应力;
            li1 = li1.ToString() == "NaN" ? 0 : li1;
            pqθ = pqθ.ToString() == "NaN" ? 0 : pqθ;
            主应力123 = 主应力123.ToString() == "NaN" ? 0 : 主应力123;
            li2 = li2.ToString() == "NaN" ? 0 : li2;
            mi2 = mi2.ToString() == "NaN" ? 0 : mi2;
            ni2 = ni2.ToString() == "NaN" ? 0 : ni2;
            主应力倾角β = 主应力倾角β.ToString() == "NaN" ? 0 : 主应力倾角β;
            主应力方位角α = 主应力方位角α.ToString() == "NaN" ? 0 : 主应力方位角α;

            return string.Format(@"INSERT INTO output_data{16}(
                batchId,sortNo,正应力,局部坐标系应力,1次变换,2次变换
                ,3次变换,大地坐标系应力,li1,pqθ,主应力123,li2,mi2,ni2,主应力倾角β
                ,主应力方位角α,channelNo)
                VALUES('{0}',{1},{2},{3},{4},{5},{6},{7}
                ,{8},{9},{10},{11},{12},{13},{14},{15},{17})"
                , batchId, sortNo, 正应力, 局部坐标系应力, ONE次变换
                , TWO变换, THREE变换, 大地坐标系应力, li1, pqθ, 主应力123
                , li2, mi2, ni2, 主应力倾角β, 主应力方位角α
                , Separation, channelNo);
        }

        public override string GetSelect(string batchId)
        {
            string Separation = string.Format("_{0}", batchId.Split('_')[0]);
            return string.Format(@"SELECT
                sortNo
                ,正应力
                ,局部坐标系应力
                ,1次变换
                ,2次变换
                ,3次变换
                ,大地坐标系应力
                ,li1
                ,pqθ
                ,主应力123
                ,li2
                ,mi2
                ,ni2
                ,主应力倾角β
                ,主应力方位角α
                ,channelNo as 通道号
                FROM output_data{1}
                WHERE batchId = '{0}'
                ORDER BY sortNo
                ", batchId
                , Separation);
        }

        public Cal应力_Result(string id, string batchId, string equipmentId, string projectId, string calType) :
            base(id, batchId, equipmentId, projectId, calType)
        { }
    }
    public class Class_Cal
    {
        #region 系数
        private double 温度补偿系数;
        private double 初始温度;
        private double[] 传感器初始波长;
        private double[] 传感器标定系数;
        private double[,] 应力变换矩阵 = {
                { -2.1546,0.5773,0.5773,2.7697,-0.3849,-0.3849},
                { 1.4879,-1.2440,-1.2440,-1.4364,1.7182,1.7182},
                { 1.0000,1.0000,1.0000,-0.6667,-0.6667,-0.6667},
                { 0.0000,1.5775,-1.5775,0.0000,-1.8215,1.8215},
                { 0.0000,1.3661,-1.3661,0.0000,-0.9107,0.9107},
                { 1.5773,-0.7886,-0.7886,-1.0515,0.5258,0.5258}
        };
        private double 方位角α;
        private double 倾角β;
        private double 旋转角γ;
        private double[,] liminiOne, liminiTwo, liminiThree, liminiFour;

        private double[] 局部坐标系应力;
        #endregion

        #region 动态值
        private double[] 传感器输出波长;
        private double[,] 方位角变换矩阵, 倾角变换矩阵, 旋转角变换矩阵, 大地变换矩阵;
        #endregion

        #region 过程值
        private double[] 正应力;
        private double 温度;
        private double[] ONE次变换, TWO次变换, THREE次变换, 大地坐标系应力;
        #endregion

        #region 结果
        private double[] li1, pqθ, 主应力123, li2, mi2, ni2, 主应力倾角β, 主应力方位角α, li3, mi3, ni3, 应变;
        #endregion

        public void 设置初始值(
            double 温度补偿系数
            , double 初始温度
            , double[] 传感器初始波长
            , double[] 传感器标定系数
            , double[] 传感器输出波长)
        {
            设置初始值(
            温度补偿系数
            , 初始温度
            , 传感器初始波长
            , 传感器标定系数
            , 0
            , 0
            , 0
            , 传感器输出波长
            , null);
        }
        public void 设置初始值(
            double 温度补偿系数
            , double 初始温度
            , double[] 传感器初始波长
            , double[] 传感器标定系数
            , double 方位角α
            , double 倾角β
            , double 旋转角γ
            , double[] 传感器输出波长
            , double[,] 应力变换矩阵)
        {
            this.温度补偿系数 = 温度补偿系数;
            this.初始温度 = 初始温度;
            this.传感器初始波长 = 传感器初始波长;
            this.传感器标定系数 = 传感器标定系数;
            this.方位角α = 方位角α;
            this.倾角β = 倾角β;
            this.旋转角γ = 旋转角γ;
            this.传感器输出波长 = 传感器输出波长;
            this.应力变换矩阵 = 应力变换矩阵;
        }

        private bool CheckData()
        {
            double Count = 0;
            for (int i = 0; i < 传感器输出波长.Length; i++)
            {
                Count += 传感器输出波长[i];
            }

            return Count == 0 ? false : true;
        }
        public Cal应变_Result Get应变(string id, string batchId, string equipmentId, string projectId)
        {
            if (!CheckData())
                return null;
            Cal应变_Result cal = new Cal应变_Result(id,
                batchId,
                equipmentId,
                projectId,
                "正应力");
            cal.温度 = this.得到温度();
            cal.正应力 = this.得到应变(this.传感器初始波长, this.传感器标定系数, this.传感器输出波长, this.温度补偿系数);
            return cal;
        }

        public Cal应力_Result Get应力(string id, string batchId, string equipmentId, string projectId)
        {
            if (!CheckData())
                return null;

            this.温度 = 得到温度();
            this.正应力 = 得到正应力();
            this.局部坐标系应力 = 得到局部坐标系应力();
            this.liminiOne = 得到liminiOne();
            this.方位角变换矩阵 = 得到角度矩阵变换(this.liminiOne);
            this.ONE次变换 = 得到矩阵变换(this.方位角变换矩阵, this.局部坐标系应力, 15);
            this.liminiTwo = 得到liminiTwo();
            this.倾角变换矩阵 = 得到角度矩阵变换(this.liminiTwo);
            this.TWO次变换 = 得到矩阵变换(this.倾角变换矩阵, this.ONE次变换, 15);
            this.liminiThree = 得到liminiThree();
            this.旋转角变换矩阵 = 得到角度矩阵变换(this.liminiThree);
            this.THREE次变换 = 得到矩阵变换(this.旋转角变换矩阵, this.TWO次变换, 15);
            this.liminiFour = 得到liminiFour();
            this.大地变换矩阵 = 得到角度矩阵变换(this.liminiFour);
            this.大地坐标系应力 = 得到矩阵变换(this.大地变换矩阵, this.THREE次变换, 15);
            this.li1 = 得到li1(this.大地坐标系应力);
            this.pqθ = 得到pqθ(this.li1);
            this.主应力123 = 得到主应力123(this.li1, this.pqθ);
            this.li2 = this.得到li2(this.大地坐标系应力, this.主应力123);
            this.mi2 = this.得到mi2(this.大地坐标系应力, this.主应力123, this.li2);
            this.ni2 = this.得到ni2(this.大地坐标系应力, this.主应力123, this.li2);
            this.主应力倾角β = this.得到主应力倾角β(this.ni2);
            this.主应力方位角α = this.得到主应力方位角α(this.mi2, this.ni2);

            Cal应力_Result cal = new Cal应力_Result(id,
                batchId,
                equipmentId,
                projectId, "正应力");
            cal.温度 = this.温度;
            cal.正应力 = this.正应力;
            cal.局部坐标系应力 = this.局部坐标系应力;
            cal.ONE次变换 = this.ONE次变换;
            cal.TWO次变换 = this.TWO次变换;
            cal.THREE次变换 = this.THREE次变换;
            cal.大地坐标系应力 = this.大地坐标系应力;
            cal.li1 = this.li1;
            cal.pqθ = this.pqθ;
            cal.主应力123 = this.主应力123;
            cal.li2 = this.li2;
            cal.mi2 = this.mi2;
            cal.ni2 = this.ni2;
            cal.主应力倾角β = this.主应力倾角β;
            cal.主应力方位角α = this.主应力方位角α;

            return cal;
        }

        #region 方法
        public void Ini()
        {
            #region 系数
            this.温度补偿系数 = 2;
            this.初始温度 = 20;
            this.传感器初始波长 = new double[] { 1525.0000, 1528.0000, 1531.0000, 1534.0000, 1537.0000, 1540.0000, 1543.0000 };
            this.传感器标定系数 = new double[] { 100.0000, 51.0000, 52.0000, 53.0000, 54.0000, 55.0000, 56.0000 };
            this.方位角α = 341.3;
            this.倾角β = -3;
            this.旋转角γ = 5;
            #endregion

            #region 动态值
            this.传感器输出波长 = new double[] { 1525.0552, 1528.2693, 1531.6611, 1534.4089, 1537.5176, 1540.5201, 1543.4017 };
            #endregion

            #region 过程值
            #endregion
        }

        public double 得到温度()
        {
            return Math.Round((this.传感器输出波长[0] - this.传感器初始波长[0]) * this.传感器标定系数[0] + this.初始温度, 2);
        }
        public double[] 得到正应力()
        {
            int len = 6;
            double[] vs = new double[len];
            for (int i = 0; i < len; i++)
            {
                vs[i] = Math.Round((this.传感器输出波长[i + 1] - this.传感器初始波长[i + 1] - this.温度补偿系数 * (this.传感器输出波长[0] - this.传感器初始波长[0])) * this.传感器标定系数[i + 1], 4);
            }
            return vs;
        }
        public double[] 得到局部坐标系应力()
        {
            return 得到矩阵变换(this.应力变换矩阵, this.正应力);
        }

        public double[,] 得到liminiFour()
        {
            int length = 3;
            double[,] vs = new double[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = 0;
                }
            }
            #region 老的
            vs[0, 0] = 1;//第二次修改
            vs[1, 2] = -1;
            vs[2, 1] = 1;
            #endregion


            vs[0, 1] = 1;//第二次修改
            vs[1, 0] = 1;
            vs[2, 2] = -1;

            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = Math.Round(vs[i, k], 15);
                }
            }
            return vs;
        }
        public double[,] 得到liminiThree()
        {
            int length = 3;
            double[,] vs = new double[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = 0;
                }
            }

            vs[0, 0] = 1;
            //COS((C21+90)/180*PI())
            vs[0, 1] = Math.Cos((this.旋转角γ + 90) / 180 * Math.PI);
            //COS(PI()*C21/180)
            vs[1, 1] = Math.Cos(Math.PI * this.旋转角γ / 180);
            //COS(PI()*(C21+90)/180)
            vs[1, 2] = Math.Cos((this.旋转角γ + 90) / 180 * Math.PI);
            //COS(PI()*(90-C21)/180)
            vs[2, 1] = Math.Cos((90 - this.旋转角γ) / 180 * Math.PI);

            vs[2, 2] = vs[1, 1];

            double t = Math.PI;

            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = Math.Round(vs[i, k], 15);
                }
            }
            return vs;
        }
        public double[,] 得到liminiTwo()
        {
            int length = 3;
            double[,] vs = new double[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = 0;
                }
            }
            //COS(PI()*B21/180)
            vs[0, 0] = Math.Cos(Math.PI * this.倾角β / 180);
            //COS(PI()*(90-B21)/180)
            vs[0, 2] = Math.Cos(Math.PI * (90 - this.倾角β) / 180);
            vs[1, 1] = 1;
            //COS(PI()*(90+B21)/180)
            vs[2, 0] = Math.Cos(Math.PI * (90 + this.倾角β) / 180);
            vs[2, 2] = vs[0, 0];

            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = Math.Round(vs[i, k], 15);
                }
            }
            return vs;
        }
        public double[,] 得到liminiOne()
        {
            int length = 3;
            double[,] vs = new double[length, length];
            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = 0;
                }
            }
            //COS(PI()*A21/180)
            vs[0, 0] = Math.Cos(Math.PI * this.方位角α / 180);
            //COS((A21+90)/180*PI())
            vs[0, 1] = Math.Cos((this.方位角α + 90) / 180 * Math.PI);
            //COS((90-A21)/180*PI())
            vs[1, 0] = Math.Cos((90 - this.方位角α) / 180 * Math.PI);
            vs[1, 1] = vs[0, 0];
            vs[2, 2] = 1;
            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = Math.Round(vs[i, k], 15);
                }
            }
            return vs;
        }

        public double[,] 得到角度矩阵变换(double[,] IniArray)
        {
            int length = 6;
            double[,] vs = new double[length, length];
            for (int col = 0; col < length; col++)
            {
                for (int row = 0; row < length; row++)
                {
                    if (row < 3)
                    {
                        if (col < 3)
                        {
                            vs[col, row] = IniArray[col, row] * IniArray[col, row];
                        }
                        if (col == 3)
                        {
                            //A23*A24
                            vs[col, row] = IniArray[0, row] * IniArray[1, row];
                        }
                        if (col == 4)
                        {
                            //A24*A25
                            vs[col, row] = IniArray[1, row] * IniArray[2, row];
                        }
                        if (col == 5)
                        {
                            //A23*A25
                            vs[col, row] = IniArray[0, row] * IniArray[2, row];
                        }
                    }
                }
            }

            //A23*A24
            vs[3, 0] = IniArray[0, 0] * IniArray[1, 0];
            //B23*B24
            vs[3, 1] = IniArray[0, 1] * IniArray[1, 1];
            //C23*C24
            vs[3, 2] = IniArray[0, 2] * IniArray[1, 2];

            //A24*A25
            vs[4, 0] = IniArray[1, 0] * IniArray[2, 0];
            //B24*B25
            vs[4, 1] = IniArray[1, 1] * IniArray[2, 1];
            //C24*C25
            vs[4, 2] = IniArray[1, 2] * IniArray[2, 2];

            //A23*A25
            vs[5, 0] = IniArray[0, 0] * IniArray[2, 0];
            //B23*B25
            vs[5, 1] = IniArray[0, 1] * IniArray[2, 1];
            //C23*C25
            vs[5, 2] = IniArray[0, 2] * IniArray[2, 2];

            //2*A23*B23
            vs[0, 3] = 2 * IniArray[0, 0] * IniArray[0, 1];
            //2*B23*C23
            vs[0, 4] = 2 * IniArray[0, 1] * IniArray[0, 2];
            //2*C23*A23
            vs[0, 5] = 2 * IniArray[0, 2] * IniArray[0, 0];
            //2*A24*B24
            vs[1, 3] = 2 * IniArray[1, 0] * IniArray[1, 1];
            vs[1, 4] = 2 * IniArray[1, 1] * IniArray[1, 2];
            vs[1, 5] = 2 * IniArray[1, 2] * IniArray[1, 0];

            vs[2, 3] = 2 * IniArray[2, 0] * IniArray[2, 1];
            vs[2, 4] = 2 * IniArray[2, 1] * IniArray[2, 2];
            vs[2, 5] = 2 * IniArray[2, 2] * IniArray[2, 0];
            //A23*B24+A24*B23
            vs[3, 3] = IniArray[0, 0] * IniArray[1, 1] + IniArray[1, 0] * IniArray[0, 1];
            //B23*C24+B24*C23
            vs[3, 4] = IniArray[0, 1] * IniArray[1, 2] + IniArray[1, 1] * IniArray[0, 2];
            //A23*C24+C23*A24
            vs[3, 5] = IniArray[0, 0] * IniArray[1, 2] + IniArray[0, 2] * IniArray[1, 0];

            //A24*B25+A25*B24
            vs[4, 3] = IniArray[1, 0] * IniArray[2, 1] + IniArray[2, 0] * IniArray[1, 1];
            //B24*C25+B25*C24
            vs[4, 4] = IniArray[1, 1] * IniArray[2, 2] + IniArray[2, 1] * IniArray[1, 2];
            //A24*C25+C24*A25
            vs[4, 5] = IniArray[1, 0] * IniArray[2, 2] + IniArray[1, 2] * IniArray[2, 0];

            //A23*B25+B23*A25
            vs[5, 3] = IniArray[0, 0] * IniArray[2, 1] + IniArray[0, 1] * IniArray[2, 0];
            //B23*C25+B25*C23
            vs[5, 4] = IniArray[0, 1] * IniArray[2, 2] + IniArray[2, 1] * IniArray[0, 2];
            //C25*A23+A25*C23
            vs[5, 5] = IniArray[2, 2] * IniArray[0, 0] + IniArray[2, 0] * IniArray[0, 2];

            for (int i = 0; i < length; i++)
            {
                for (int k = 0; k < length; k++)
                {
                    vs[i, k] = Math.Round(vs[i, k], 15);
                }
            }
            return vs;
        }
        public double[] 得到主应力123(double[] vs1, double[] vs2)
        {
            double[] vs = new double[6];
            //L44/3+2*COS(M46/3)*(-M44/3)^0.5
            vs[0] = vs1[0] / 3 + 2 * Math.Cos(vs2[2] / 3) * Math.Pow((-vs2[0] / 3), 0.5);
            //L44/3+2*COS(M46/3+4*PI()/3)*(-M44/3)^0.5
            vs[1] = vs1[0] / 3 + 2 * Math.Cos(vs2[2] / 3 + 4 * Math.PI / 3) * Math.Pow((-vs2[0] / 3), 0.5);
            //L44/3+2*COS(M46/3+2*PI()/3)*(-M44/3)^0.5
            vs[2] = vs1[0] / 3 + 2 * Math.Cos(vs2[2] / 3 + 2 * Math.PI / 3) * Math.Pow((-vs2[0] / 3), 0.5);
            for (int i = 0; i < vs.Length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到li2(double[] vs1, double[] vs2)
        {
            double[] vs = new double[6];
            for (int i = 0; i < 6; i++)
            {
                if (i < 3)
                    vs[i] = Math.Abs((vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[i])) /
                        Math.Pow(Math.Pow(vs1[3] * vs1[5] - vs1[4] * (vs1[0] - vs2[i]), 2)
                        + Math.Pow(vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[i]), 2)
                        + Math.Pow((vs1[0] - vs2[i]) * (vs1[1] - vs2[i]) - vs1[3] * vs1[3], 2)
                        , 0.5));
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到mi2(double[] vs1, double[] vs2, double[] vs3)
        {
            double[] vs = new double[6];
            //(K47*K49-K48*(K44-N44))*O44/(K47*K48-K49*(K45-N44))
            vs[0] = (vs1[3] * vs1[5] - vs1[4] * (vs1[0] - vs2[0])) * vs3[0] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[0]));
            //(K47*K49-K48*(K44-N45))*O45/(K47*K48-K49*(K45-N45))
            vs[1] = (vs1[3] * vs1[5] - vs1[4] * (vs1[0] - vs2[1])) * vs3[1] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[1]));
            //(K47*K49-K48*(K44-N46))*O46/(K47*K48-K49*(K45-N46))
            vs[2] = (vs1[3] * vs1[5] - vs1[4] * (vs1[0] - vs2[2])) * vs3[2] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[2]));
            for (int i = 0; i < vs.Length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到ni2(double[] vs1, double[] vs2, double[] vs3)
        {
            int index = 0;
            //--这里有除法
            double[] vs = new double[6];
            if (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]) == 0)
                return vs;
            index++;
            if (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]) == 0)
                return vs;
            index++;
            if (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]) == 0)
                return vs;

            index = 0;

            //((K44-N44)*(K45-N44)-K47*K47)*O44/(K47*K48-K49*(K45-N44))
            vs[index] = ((vs1[0] - vs2[index]) * (vs1[1] - vs2[index]) - vs1[3] * vs1[3]) * vs3[index] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]));
            index++;
            //((K44-N45)*(K45-N45)-K47*K47)*O45/(K47*K48-K49*(K45-N45))
            vs[index] = ((vs1[0] - vs2[index]) * (vs1[1] - vs2[index]) - vs1[3] * vs1[3]) * vs3[index] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]));
            index++;
            //((K44-N46)*(K45-N46)-K47*K47)*O46/(K47*K48-K49*(K45-N46))
            vs[index] = ((vs1[0] - vs2[index]) * (vs1[1] - vs2[index]) - vs1[3] * vs1[3]) * vs3[index] / (vs1[3] * vs1[4] - vs1[5] * (vs1[1] - vs2[index]));
            for (int i = 0; i < vs.Length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到主应力倾角β(double[] vs1)
        {
            double[] vs = new double[6];
            if (Math.Asin(vs1[0]) == 0)
            {
                return vs;
            }
            int index = 0;
            //--这里有除法
            //180/PI()*ASIN(Q44)
            vs[index] = 180 * Math.Asin(vs1[index]) / Math.Round(Math.PI, 14);
            index++;
            //180*ASIN(Q45)/PI()
            vs[index] = 180 * Math.Asin(vs1[index]) / Math.Round(Math.PI, 14);
            index++;
            //180*ASIN(Q46)/PI()
            vs[index] = 180 * Math.Asin(vs1[index]) / Math.Round(Math.PI, 14);
            for (int i = 0; i < vs.Length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到主应力方位角α(double[] vs1, double[] vs2)
        {
            int index = 0;
            double[] vs = new double[6];
            //--这里有除法
            if (1 - vs2[index] * vs2[index] == 0)
                return vs;
            index++;
            if (1 - vs2[index] * vs2[index] == 0)
                return vs;
            index++;
            if (1 - vs2[index] * vs2[index] == 0)
                return vs;

            index = 0;
            //-180/PI()*ASIN(vs1[index]/(1-vs2[index]*vs2[index])^0.5)
            vs[index] = -180 / Math.PI * Math.Asin(vs1[index] / Math.Pow(1 - vs2[index] * vs2[index], 0.5));
            index++;
            //-180/PI()*ASIN(P45/(1-Q45*Q45)^0.5)
            vs[index] = -180 / Math.PI * Math.Asin(vs1[index] / Math.Pow(1 - vs2[index] * vs2[index], 0.5));
            index++;
            //-180/PI()*ASIN(P46/(1-Q46*Q46)^0.5)
            vs[index] = -180 / Math.PI * Math.Asin(vs1[index] / Math.Pow(1 - vs2[index] * vs2[index], 0.5));
            index++;
            for (int i = 0; i < vs.Length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public double[] 得到应变(double[] C, double[] D, double[] E, double temp)
        {
            double[] vs = new double[6];
            //(E3-C3-$B$1*($E$2-$C$2))*D3
            //(vs3[index]-vs1[index]-temp*(vs3[0]-vs1[0]))*vs2[index]
            for (int index = 0; index < vs.Length; index++)
            {
                vs[index] = (E[index + 1] - C[index + 1] - temp * (E[0] - C[0])) * D[index + 1];
            }
            for (int i = 0; i < vs.Length; i++)
            {
                vs[i] = Math.Round(vs[i], 15);
            }
            return vs;
        }
        public double[] 得到pqθ(double[] IniArray)
        {
            int length = 6;
            double[] vs = new double[length];
            //L45-L44*L44/3
            vs[0] = IniArray[1] - IniArray[0] * IniArray[0] / 3;
            //L44*L45/3-L46-2*(L44^3)/27
            vs[1] = IniArray[0] * IniArray[1] / 3 - IniArray[2] - 2 * (Math.Pow(IniArray[0], 3)) / 27;
            //Acos((M45*0.5/(-M44*M44*M44/27)^0.5))
            //Acos((M45*0.5/(-M44*M44*M44/27)^0.5))//第二次修改，新的就是乘以了负壹
            vs[2] = Math.Acos(vs[1] * 0.5 / Math.Pow((-vs[0] * vs[0] * vs[0] / 27), 0.5));
            vs[2] = Math.Acos(-1 * vs[1] * 0.5 / Math.Pow((-vs[0] * vs[0] * vs[0] / 27), 0.5));
            for (int i = 0; i < length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;

        }
        public double[] 得到li1(double[] IniArray)
        {
            int length = 6;
            double[] vs = new double[length];
            //K44+K45+K46
            vs[0] = IniArray[0] + IniArray[1] + IniArray[2];
            //K44*K45+K45*K46+K46*K44-K47*K47-K48*K48-K49*K49
            vs[1] = IniArray[0] * IniArray[1]
                + IniArray[1] * IniArray[2]
                + IniArray[2] * IniArray[0]
                - IniArray[3] * IniArray[3]
                - IniArray[4] * IniArray[4]
                - IniArray[5] * IniArray[5];
            //K44*K45*K46-2*K47*K48*K49-K44*K48*K48-K45*K49*K49-K46*K47*K47
            vs[2] = IniArray[0] * IniArray[1] * IniArray[2]
                - 2 * IniArray[3] * IniArray[4] * IniArray[5]
                - IniArray[0] * IniArray[4] * IniArray[4]
                - IniArray[1] * IniArray[5] * IniArray[5]
                - IniArray[2] * IniArray[3] * IniArray[3];

            for (int i = 0; i < length; i++)
            {
                if (i < 3)
                    vs[i] = Math.Round(vs[i], 15);
                else
                    vs[i] = 0;
            }
            return vs;
        }
        public string GetArray(double[] array, int round)
        {
            if (array == null)
                return null;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                if (i > 0)
                    stringBuilder.Append(", ");
                stringBuilder.Append(AddString(Math.Round(array[i], round), 7));
            }
            return stringBuilder.ToString();
        }
        public string GetArray(double[,] array, int size, int round)
        {
            if (array == null)
                return null;
            StringBuilder stringBuilder = new StringBuilder();
            double[,] vs = array;
            for (int i = 0; i < size; i++)
            {
                for (int k = 0; k < size; k++)
                {
                    if (k > 0)
                        stringBuilder.Append(", ");
                    stringBuilder.Append(AddString(Math.Round(vs[i, k], round), 7));
                }
                stringBuilder.Append("\r\n");
            }
            return stringBuilder.ToString();
        }
        #endregion

        private string AddString(double temp, int len)
        {
            string s = null;
            int index = 0;
            while (index < len - temp.ToString().Length)
            {
                s += " ";
                index++;
            }
            return string.Format("{0}{1}", s, temp.ToString());
        }
        private double[] 得到矩阵变换(double[,] IniArray, double[] SimArray)
        {
            return 得到矩阵变换(IniArray, SimArray, 15);
        }
        private double[] 得到矩阵变换(double[,] IniArray, double[] SimArray, int Round)
        {
            int length = 6;
            double[] vs = new double[length];
            for (int i = 0; i < length; i++)
            {
                double temp = 0;
                for (int k = 0; k < length; k++)
                {
                    temp += IniArray[i, k] * SimArray[k];
                }
                vs[i] = Math.Round(temp, Round);
            }
            return vs;
        }

        public void button1_Click(object sender, EventArgs e)
        {
            #region
            //this.温度 = 得到温度();
            //this.正应力 = 得到正应力();
            //this.textBox1.Text = GetArray(this.正应力, 4);

            //this.局部坐标系应力 = 得到局部坐标系应力();
            //this.textBox2.Text = GetArray(this.局部坐标系应力, 4);

            //this.liminiOne = 得到liminiOne();
            //this.textBox3.Text = GetArray(this.liminiOne, 3, 4);

            //this.方位角变换矩阵 = 得到角度矩阵变换(this.liminiOne);
            //this.textBox4.Text = GetArray(this.方位角变换矩阵, 6, 4);

            //this.ONE次变换 = 得到矩阵变换(this.方位角变换矩阵, this.局部坐标系应力, 15);
            //this.textBox5.Text = GetArray(this.ONE次变换, 2);

            //this.liminiTwo = 得到liminiTwo();
            //this.textBox6.Text = GetArray(this.liminiTwo, 3, 4);

            //this.倾角变换矩阵 = 得到角度矩阵变换(this.liminiTwo);
            //this.textBox7.Text = GetArray(this.倾角变换矩阵, 6, 4);

            //this.TWO次变换 = 得到矩阵变换(this.倾角变换矩阵, this.ONE次变换, 15);
            //this.textBox8.Text = GetArray(this.TWO次变换, 2);

            //this.liminiThree = 得到liminiThree();
            //this.textBox9.Text = GetArray(this.liminiThree, 3, 4);

            //this.旋转角变换矩阵 = 得到角度矩阵变换(this.liminiThree);
            //this.textBox10.Text = GetArray(this.旋转角变换矩阵, 6, 4);

            //this.THREE次变换 = 得到矩阵变换(this.旋转角变换矩阵, this.TWO次变换, 15);
            //this.textBox11.Text = GetArray(this.THREE次变换, 2);

            //this.liminiFour = 得到liminiFour();
            //this.大地变换矩阵 = 得到角度矩阵变换(this.liminiFour);
            //this.textBox12.Text = GetArray(this.大地变换矩阵, 6, 4);

            //this.大地坐标系应力 = 得到矩阵变换(this.大地变换矩阵, this.THREE次变换, 15);
            //this.textBox13.Text = GetArray(this.大地坐标系应力, 2);

            //this.Ii1 = 得到Ii1(this.大地坐标系应力);
            //this.textBox14.Text = GetArray(this.Ii1, 2);

            //this.pqθ = 得到pqθ(this.Ii1);
            //this.textBox15.Text = GetArray(this.pqθ, 2);

            //this.主应力123 = 得到主应力123(this.Ii1, this.pqθ);
            //this.textBox16.Text = GetArray(this.主应力123, 2);

            //this.li2 = this.得到li2(this.大地坐标系应力, this.主应力123);
            //this.textBox17.Text = GetArray(this.li2, 2);

            //this.mi2 = this.得到mi2(this.大地坐标系应力, this.主应力123, this.li2);
            //this.textBox18.Text = GetArray(this.mi2, 2);

            //this.ni2 = this.得到ni2(this.大地坐标系应力, this.主应力123, this.li2);
            //this.textBox19.Text = GetArray(this.ni2, 2);

            //this.主应力倾角β = this.得到主应力倾角β(this.ni2);
            //this.textBox20.Text = GetArray(this.主应力倾角β, 2);

            //this.主应力方位角α = this.得到主应力方位角α(this.mi2, this.ni2);
            //this.textBox21.Text = GetArray(this.主应力方位角α, 2);
            #endregion

            //this.li3 = null;
            //this.textBox22.Text = GetArray(this.li3);
            //this.mi3 = null;
            //this.textBox23.Text = GetArray(this.mi3);
            //this.ni3 = null;
            //this.textBox24.Text = GetArray(this.ni3);
        }
        public void button2_Click(object sender, EventArgs e)
        {
            //this.应变 = 得到应变(this.传感器初始波长, this.传感器标定系数, this.传感器输出波长, this.温度补偿系数);
            //this.textBox25.Text = GetArray(this.应变, 4);
        }
    }
}
